package com.fulihui.wgj.admin.api.wxtemplate;

import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendLocalTemplateRequest;
import com.fulihui.wgj.admin.integration.SendTemplateLogServiceClient;
import com.fulihui.wgj.admin.integration.WxLocalTemplateServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-26 13:44
 */
@RestController
@RequestMapping("/local")
@Api(tags = "本地微信模板消息相关接口")
public class SendLocalTemplateController extends BaseController {

    @Autowired
    private WxLocalTemplateServiceClient wxLocalTemplateServiceClient;

    @Autowired
    private SendTemplateLogServiceClient sendTemplateLogServiceClient;

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ApiOperation("发送本地模板消息")
    public Result<Boolean> send(@RequestBody @Valid SendLocalTemplateRequest request, BindingResult validResult,
                                HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return wxLocalTemplateServiceClient.send(request);
    }

}
